package com.dbh.alg.datastructure;

/**
 * 判断链表是否有环
 * @author 董帮辉
 * @date 2025/5/11
 **/
public class E10Leetcode141_判断链表是否有环 {

    public static boolean hasCycle(ListNode head) {
        ListNode p1 = head;
        ListNode p2 = head;
        while (p2 != null && p2.next != null) {
            p1 = p1.next;
            p2 = p2.next.next;
            if (p1 == p2) {
                return true;
            }
        }
        return false;
    }
}
